/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.web.open.app.user;

import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.model.ButcherFactory;
import com.jiantong.user.service.ButcherFactoryReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author : panxin
 */
@Slf4j
@OpenBean
public class ButcherFactoryApis {

    @RpcConsumer
    private ButcherFactoryReadService butcherFactoryReadService;


    @OpenMethod(
            key = "find.butcher.factory.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public ButcherFactory findButcherFactoryById(Long id){
        Response<ButcherFactory> response = butcherFactoryReadService.findButcherFactoryById(id);
        if(!response.isSuccess()){
            log.error("failed.to.find.butcherFactory id = {} , case = {}",id,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "find.local.butcher.factories",
            httpMethods = RequestMethod.GET
    )
    public List<ButcherFactory> findLocalButcherFactories(){
        LoginUser loginUser = UserUtil.getCurrentUser();
        if(loginUser == null){
            log.error("user is not login");
            throw new OPClientException("user is not login");
        }
        Response<List<ButcherFactory>> response = butcherFactoryReadService.findByCityId(loginUser);
        if(!response.isSuccess()){
            log.error("failed.to.find.butcherFactories, case = {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}
